package gspushmq.v1.subscribe.handler.core;

import java.util.Iterator;

import com.alibaba.fastjson.JSONArray;

import gspushmq.model.ApiMessageType;
import gspushmq.v1.produce.GGDataStorage;
import gspushmq.v1.subscribe.GGSubscribe;
import gspushmq.v1.subscribe.handler.GGAccountIdsHandlerImpl;
import gspushmq.v1.subscribe.handler.GGAiHandlerImpl;
import gspushmq.v1.thread.GGThreadPool;


/**
 * 逻辑处理程序
 *
 * @author yutao
 * @version 1.0
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2016 SunTime Co'Ltd Inc.All rights reserved.
 * @date 2018年11月6日下午2:42:59
 * @since JDK1.7
 */
public class GGHandlerPush extends GGHandler{

	private void start() {
		while(true){
			if(GGSubscribe.apiHandlerQueue.isEmpty()){
				try {
					Thread.sleep(800);
					continue;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			Iterator<ApiMessageType> iterator = GGSubscribe.apiHandlerQueue.iterator();
			
			while(iterator.hasNext()) {
				//生产的某一类消息
				ApiMessageType o = iterator.next();
				
				String apiUrl = o.getApi_url();
				JSONArray array = GGDataStorage.ApiListMap.get(o);
				if("v1/tg/get_ai_event_catalysts_list".equals(apiUrl)) {
					//说明需要处理业务逻辑
					
					GGAiHandlerImpl aiHandler = new GGAiHandlerImpl();
					aiHandler.setArray(array);
					aiHandler.setApiType(o);
					GGThreadPool.threadPool.execute(aiHandler);
				}else {
					//多账号处理
					
					GGAccountIdsHandlerImpl aidsHandler = new GGAccountIdsHandlerImpl();
					aidsHandler.setApiType(o);
					aidsHandler.setArray(array);
					GGThreadPool.threadPool.execute(aidsHandler);
				}
				GGSubscribe.apiHandlerQueue.remove(o);
			}
		}
	}

	/**
	 * 
	 * @see gspushmq.v1.subscribe.handler.core.GGHandler#handler()
	 */
	@Override
	public void handler() {
		start();
	}
}
